﻿Imports DAL
Public Class UCParametres

    'Paramètre de démarage de la page de paramètres
    Private Sub UCParametres_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load, Me.VisibleChanged
        If Not Me.DesignMode Then
            'Insertion des bénéficiaires, précédament supprimé, dans la liste de bénéficiaire.
            LVBeneficiaireSupprimes.Items.Clear()
            For Each beneficiaireSupprime In (From b In bd.Beneficiaires Where b.Supprime = True Select b)
                Dim item As New ListViewItem
                item.Text = beneficiaireSupprime.Nom + ", " + beneficiaireSupprime.Prenom
                item.SubItems.Add(beneficiaireSupprime.DateSupprime)
                item.Tag = beneficiaireSupprime.ID
                LVBeneficiaireSupprimes.Items.Add(item)
            Next

            'Insertion des Employés, précédament supprimé, dans la liste d'employé.
            LvEmpSup.Items.Clear()
            For Each employéSupprime In (From b In bd.Employes Where b.Supprime = True Select b)
                Dim item As New ListViewItem
                item.Text = employéSupprime.Nom + ", " + employéSupprime.Prenom
                item.SubItems.Add(employéSupprime.SupprimeDate)
                item.Tag = employéSupprime.ID
                LvEmpSup.Items.Add(item)
            Next
            RemplireListePlage()
            If (From i In bd.Parametres Select i.CouleurAccompagnement).Count > 0 Then
                Pb_CouleurPlage.BackColor = ColorTranslator.FromHtml(bd.Parametres.First.CouleurAccompagnement)
            Else
                Pb_CouleurPlage.BackColor = ColorTranslator.FromHtml("#dddddd")
            End If

            For Each Affinite In bd.Affinites
                Dim item As New ListViewItem
                item.Text = Affinite.Nom
                item.Tag = Affinite.ID
                LvAffinite.Items.Add(item)
            Next
            For Each Fromation In bd.Formations
                Dim item As New ListViewItem
                item.Text = Fromation.Nom
                item.Tag = Fromation.ID
                LvFormation.Items.Add(item)
            Next
        End If
    End Sub
    Private Sub RemplireListePlage()
        Lv_Plages.Items.Clear()
        Dim imglist As New ImageList
        imglist.ImageSize = New Size(20, 20)
        Dim ctr As Integer = 0
        Using nbd As New BDCarpeDiemDataContext
            For Each plage As PlageHoraireStocke In From x In nbd.PlageHoraireStockes Select x

                Dim itm As New ListViewItem
                itm.Text = plage.Nom
                itm.ImageIndex = ctr
                itm.Tag = plage.ID
                ctr += 1
                Lv_Plages.Items.Add(itm)
                Dim img As New Bitmap(20, 20)
                Using g As Graphics = Graphics.FromImage(img)
                    g.Clear(System.Drawing.ColorTranslator.FromHtml(plage.Couleur))
                End Using
                imglist.Images.Add(img)

            Next
            Lv_Plages.SmallImageList = imglist
        End Using
    End Sub
    'Gestion de l'intéraction avec le bouton restaurer
    Private Sub Restaurer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRestaurer.Click, btnRestaurerEmp.Click
        If TabBeneficiaire.Focus = True Then
            'Remise en fonction d'un bénéficiaire précédament supprimé et mis de côté
            For Each beneficiaireARestaure In (From b In bd.Beneficiaires Where (From i As ListViewItem In LVBeneficiaireSupprimes.SelectedItems Select i.Tag).Contains(b.ID) Select b)
                beneficiaireARestaure.Supprime = False
                beneficiaireARestaure.DateSupprime = Nothing
            Next
            For Each item In LVBeneficiaireSupprimes.SelectedItems
                LVBeneficiaireSupprimes.Items.Remove(item)
            Next
        ElseIf TabEmployes.Focus = True Then
            'Remise en fonction d'un employé précédament supprimé et mis de côté
            For Each employéARestaure In (From b In bd.Employes Where (From i As ListViewItem In LvEmpSup.SelectedItems Select i.Tag).Contains(b.ID) Select b)
                employéARestaure.Supprime = False
                employéARestaure.SupprimeDate = Nothing
            Next
            For Each item In LvEmpSup.SelectedItems
                LvEmpSup.Items.Remove(item)
            Next
        End If
        bd.SubmitChanges()
    End Sub

    'gestion de l'intéraction avec le bouton supprimer
    Private Sub BtnSupprimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSupprimer.Click, BtnSupprimerEmp.Click
        If TabBeneficiaire.Focus = True AndAlso MsgBox("Voulez-vous vraiment supprimer définitivement ce bénéficiaire, tout les accompagements associés seront supprimés", vbYesNo, "Avertissement") = vbYes Then
            'Suppression définitive du bénéficiaire choisi dans la liste des supprimés
            bd.Beneficiaires.DeleteAllOnSubmit(From b In bd.Beneficiaires Where (From i As ListViewItem In LVBeneficiaireSupprimes.SelectedItems Select i.Tag).Contains(b.ID) Select b)
            For Each item In LVBeneficiaireSupprimes.SelectedItems
                LVBeneficiaireSupprimes.Items.Remove(item)
            Next
        ElseIf TabEmployes.Focus = True AndAlso MsgBox("Voulez-vous vraiment supprimer définitivement cet employé, tout les plages associé seront supprimés", vbYesNo, "Avertissement") = vbYes Then
            'Suppression définitive de l'employé choisi dans la liste des supprimés
            bd.Employes.DeleteAllOnSubmit(From b In bd.Employes Where (From i As ListViewItem In LvEmpSup.SelectedItems Select i.Tag).Contains(b.ID) Select b)
            For Each item In LvEmpSup.SelectedItems
                LvEmpSup.Items.Remove(item)
            Next
        End If
        bd.SubmitChanges()
    End Sub

    Private Sub Btn_AddPlageStock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_AddPlageStock.Click

        Using frm As New FrmEditionPlage()
            frm.ModeEdition = FrmEditionPlage.TypeEdition.AjoutPlageStock
            If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Using nbd As New BDCarpeDiemDataContext
                    Dim nPlageStock As New PlageHoraireStocke()
                    nPlageStock.Nom = frm.Titre
                    nPlageStock.Description = frm.Description
                    nPlageStock.Duree = frm.Duree.TotalMinutes
                    nPlageStock.HeureDebut = frm.DateDebut
                    nPlageStock.Couleur = ColorTranslator.ToHtml(frm.Couleur)
                    nbd.PlageHoraireStockes.InsertOnSubmit(nPlageStock)
                    nbd.SubmitChanges()
                End Using
                RemplireListePlage()
            End If
        End Using
    End Sub

    Private Sub Btn_ModifierPlageStock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ModifierPlageStock.Click
        If Lv_Plages.SelectedItems.Count = 1 Then
            Using frm As New FrmEditionPlage()
                frm.ModeEdition = FrmEditionPlage.TypeEdition.AjoutPlageStock
                Using nbd As New BDCarpeDiemDataContext
                    Dim plageStock As PlageHoraireStocke = (From x As PlageHoraireStocke In nbd.PlageHoraireStockes Where x.ID = CType(Lv_Plages.SelectedItems(0).Tag, Integer) Select x).First
                    frm.Titre = plageStock.Nom
                    frm.Description = plageStock.Description
                    frm.Couleur = ColorTranslator.FromHtml(plageStock.Couleur)
                    frm.Duree = TimeSpan.FromMinutes(plageStock.Duree)
                    frm.DateDebut = plageStock.HeureDebut
                    If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then

                        plageStock.Nom = frm.Titre
                        plageStock.Description = frm.Description
                        plageStock.Duree = frm.Duree.TotalMinutes
                        plageStock.HeureDebut = frm.DateDebut
                        plageStock.Couleur = ColorTranslator.ToHtml(frm.Couleur)

                        nbd.SubmitChanges()
                        RemplireListePlage()
                    End If
                End Using
            End Using
        End If
    End Sub

    Private Sub BtSupprimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtSupprimer.Click
        If Lv_Plages.SelectedItems.Count > 0 AndAlso MsgBox("Voulez-vous vraiment supprimer cette plage", vbYesNo, "Avertissement") = vbYes Then
            bd.PlageHoraireStockes.DeleteOnSubmit((From p In bd.PlageHoraireStockes Where p.ID = Convert.ToInt32(Lv_Plages.SelectedItems(0).Tag) Select p).First)
            bd.SubmitChanges()
            Lv_Plages.Items.Remove(Lv_Plages.SelectedItems(0))
        End If
    End Sub

    Private Sub Lv_Plages_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Lv_Plages.SelectedIndexChanged
        If Lv_Plages.SelectedItems.Count > 0 Then
            Btn_ModifierPlageStock.Enabled = True
            BtSupprimer.Enabled = True
        Else
            Btn_ModifierPlageStock.Enabled = False
            BtSupprimer.Enabled = False
        End If
    End Sub

    Private Sub Btn_ColorPicker_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ColorPicker.Click
        Dim diag As New FrmColorPicker()
        diag.Value = Pb_CouleurPlage.BackColor
        If diag.ShowDialog = DialogResult.OK Then
            bd.Parametres.First.CouleurAccompagnement = ColorTranslator.ToHtml(diag.Value)
            Pb_CouleurPlage.BackColor = diag.Value
            bd.SubmitChanges()
        End If
    End Sub

    Private Sub BtnAInterets_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAInterets.Click
        If Not TbAffinite.Text = String.Empty AndAlso (From a In bd.Affinites Where a.Nom = TbAffinite.Text Select a).Count = 0 Then
            Dim affinite As New Affinite
            affinite.Nom = TbAffinite.Text
            bd.Affinites.InsertOnSubmit(affinite)
            bd.SubmitChanges()
            Dim item As New ListViewItem
            item.Text = affinite.Nom
            item.Tag = affinite.ID
            LvAffinite.Items.Add(item)
        End If
        TbAffinite.Text = String.Empty
    End Sub
    Private Sub BtnEInterets_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEInterets.Click
        If LvAffinite.SelectedItems.Count > 0 Then
            If MsgBox("Voulez-vous vraiment supprimer cette affinité", vbYesNo, "Avertissement") = vbYes Then
                If ((From b In bd.BeneficiaireAffinites Where b.Affinite = Convert.ToInt32(LvAffinite.SelectedItems(0).Tag) Select b).Count = 0 And (From a In bd.EmployeAffinites Where a.Affinite = Convert.ToInt32(LvAffinite.SelectedItems(0).Tag) Select a).Count = 0) OrElse MsgBox("Cet intérêts est utilisé voulez-vous la supprimer", vbYesNo, "Avertissement") = vbYes Then
                    bd.Affinites.DeleteOnSubmit((From a In bd.Affinites Where a.ID = Convert.ToInt32(LvAffinite.SelectedItems(0).Tag) Select a).First)
                    LvAffinite.Items.Remove(LvAffinite.SelectedItems(0))
                    bd.SubmitChanges()
                End If
            End If
        End If
    End Sub
Private Sub LvEmpSup_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LvEmpSup.SelectedIndexChanged
        If LvEmpSup.SelectedItems.Count > 0 Then
            btnRestaurerEmp.Enabled = True
            BtnSupprimerEmp.Enabled = True
        Else
            btnRestaurerEmp.Enabled = False
            BtnSupprimerEmp.Enabled = False
        End If
    End Sub

    Private Sub LVBeneficiaireSupprimes_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LVBeneficiaireSupprimes.SelectedIndexChanged
        If LVBeneficiaireSupprimes.SelectedItems.Count > 0 Then
            BtnRestaurer.Enabled = True
            BtnSupprimer.Enabled = True
        Else
            BtnRestaurer.Enabled = False
            BtnSupprimer.Enabled = False
        End If
    End Sub

    Private Sub BtAjouteFormation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtAjouteFormation.Click
        If Not TbFormation.Text = String.Empty AndAlso (From a In bd.Formations Where a.Nom = TbFormation.Text Select a).Count = 0 Then
            Dim formation As New Formation
            formation.Nom = TbFormation.Text
            bd.Formations.InsertOnSubmit(formation)
            bd.SubmitChanges()
            Dim item As New ListViewItem
            item.Text = formation.Nom
            item.Tag = formation.ID
            LvFormation.Items.Add(item)
        End If
        TbFormation.Text = String.Empty
    End Sub

    Private Sub BtEnleverFormation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtEnleverFormation.Click
        If LvFormation.SelectedItems.Count > 0 Then
            If MsgBox("Voulez-vous vraiment supprimer cette formation", vbYesNo, "Avertissement") = vbYes Then
                If ((From a In bd.EmployeFormations Where a.Formation = Convert.ToInt32(LvFormation.SelectedItems(0).Tag) Select a).Count = 0) OrElse MsgBox("Cette formation est utilisé voulez-vous la supprimer", vbYesNo, "Avertissement") = vbYes Then
                    bd.Formations.DeleteOnSubmit((From a In bd.Formations Where a.ID = Convert.ToInt32(LvFormation.SelectedItems(0).Tag) Select a).First)
                    LvFormation.Items.Remove(LvFormation.SelectedItems(0))
                    bd.SubmitChanges()
                End If
            End If
        End If
    End Sub

    Private Sub TbAffinite_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TbAffinite.TextChanged
        BtnAInterets.Enabled = IIf(TbAffinite.TextLength = 0, False, True)
    End Sub

    Private Sub LvAffinite_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LvAffinite.SelectedIndexChanged
        BtnEInterets.Enabled = IIf(LvAffinite.SelectedItems.Count = 0, False, True)
    End Sub

    Private Sub TbFormation_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TbFormation.TextChanged
        BtAjouteFormation.Enabled = IIf(TbFormation.TextLength = 0, False, True)
    End Sub

    Private Sub LvFormation_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LvFormation.SelectedIndexChanged
        BtEnleverFormation.Enabled = IIf(LvFormation.SelectedItems.Count = 0, False, True)
    End Sub

End Class
